//
//  PHVisitorView.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/19.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit
import SnapKit



class PHVisitorView: UIView {
    

    
    //声明一个闭包
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //实现监听方法
    @objc private func loginClick() {
        

        closure?()
    }
    
    //该方法被外界调用,判断页面的子控件内容
    func getVisistorViewInfo(imageName:String?,title:String?) {
        
        if let img = imageName,let tit = title {
            
            iconImageView.image = UIImage(named: img)
            messageLabe.text = tit
            
            feedImageView.hidden = true
        }else {
            setupAnim()
        }
        
    }
    
    //设置动画
    func setupAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 10
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        //当切换控制器或者退出到后台,动画会终止
        anim.removedOnCompletion = false
        
        feedImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    func setupUI() {
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        //添加子控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabe)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabe.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(230)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabe)
            make.size.equalTo(CGSizeMake(100, 36))
            make.top.equalTo(messageLabe.snp_bottom).offset(16)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabe)
            make.size.equalTo(loginBtn)
            make.top.equalTo(loginBtn)
        }
        
        
    }
    
    //懒加载子控件
    private lazy var feedImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))//圆圈
    
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))//遮罩
    
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))//房子
    private lazy var messageLabe: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        return label
    }()
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()

}









